package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import persistence.CustomerDao;
import persistence.OfferDao;
import persistence.TradeDao;

/**
 * Servlet implementation class SetMode
 */
@WebServlet("/SetMode")
public class SetMode extends MyHttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	//TODO Connect the Database
	private void process(HttpServletRequest request,
			HttpServletResponse response) {
		Document xmlDoc = getDocument(request);
		NodeList modeNode = xmlDoc.getElementsByTagName("TaskMode").item(0).getChildNodes();
		String mode = modeNode.item(0).getFirstChild().getNodeValue();
		long id =  Long.parseLong((String)modeNode.item(1).getFirstChild().getNodeValue());
		if (mode.equals("pass")) {
		//FIXME Change task status to FINISHED
			OfferDao.modify(id, 1);
		}else if (mode.equals("waiting")) {
			OfferDao.modify(id, 0);
		}else if (mode.equals("nopass")) {
			OfferDao.modify(id, -1);
		}
		
	}
}
